<?php
require_once("Accountant.class.php");
require_once("exceptions/AppExceptions.class.php");

class ChukuFileAccountant extends Accountant {

	/**
	 * 文件Id
	 *
	 * @var int
	 */
	private $fileId = null;
	
	private $certificationNumber = array();

	private $certificationCodification = array();
	
	private $sequenceNumber = array();
	 
	private $manufactory = array();
	
	private $authenticationMark = array();
	
	private $steelNumber =  array();
	
	private $size1 = array();
	
	private $size2 = array();
	
	private $size3 = array();
	
	private $count = array();
	
	private $unitWeight = array();
	
	private $orderNumber = array();
	
	private $orderSubitemNumber = array();
	
	private $unitPrice = array();
	
	private $materialNumber = array();
	
	private $consignmentBatch = array();
	
	private $sampleBatch = array();
	
	private $facheNumber = array();
	
	private $purchaseNumber = array();
	
	private $shipmentDate = array();
	
	private $month = array();
	
	private $vehicleNumber = array();
	
	private $consignee = array();
	
	private $remark = array();
	
	private $handler = array();
	
	private $haveCn = false;
	
	private $deleteSql = array();
	/**
	 * 构造函数，根据文件名称初始化sql语句。
	 *
	 * @param string $filename
	 */
	public function __construct($fileId){
		
		$query = "select * from ((select certificationNumber,handler from little_chuku where fileId = '{$fileId}') as chukuTable
					left outer join 
					(select * from little_main) as mainTable
					using (certificationNumber))";
		DB::query($query);
		$result = DB::getResult();
		
		$tableCols = array(
				'产品质量证明书号' 	=> 	'certificationNumber',
				'产品质量证明书编号' 		=> 	'certificationCodification',
				'批次' 		=> 	'sequenceNumber',
				'生产厂家' 		=> 	'manufactory',
				'船级' 		=> 	'authenticationMark',
				'材质' 		=> 	'steelNumber',
				'厚' 		=> 	'size1',
				'宽' 	=> 	'size2',
				'长' 	=> 	'size3',
				'数量'	=>	'count',
				'单重'		=>	'unitWeight',
				'订单号'		=>	'orderNumber',
				'订单子项号'		=>	'orderSubitemNumber',
				'受订单价'		=>	'unitPrice',
				'物料号'		=>  'materialNumber',
				'发货批次'	=>  'consignmentBatch',
				'批号'		=>  'sampleBatch',
				'发车证书号'	=>  'facheNumber',
				'购单号'	=>  'purchaseNumber',
				'发货时间'	=>  'shipmentDate',
				'合同月份'	=>  'month',
				'车号'	=>  'vehicleNumber',
				'销售产家'	=>  'consignee',
				'经手人'		=>  'handler',
				'备注'	=>  'remark'
		);
		$cols = array();
		if($row = $result->fetch_assoc()){
			foreach($tableCols as $key => $val){
				if(isset($row[$val]) and !empty($row[$val])){
					array_push($cols, $val);
				}
			}
		}
		
		$this->haveCn = false;
		if(in_array('certificationNumber', $cols)){
			$colswhere = 'certificationNumber';
			$this->haveCn = true;
		}
		
		$colswhere = join(',', $cols);
		
		if(empty($colswhere)){
			$this->sql = null;
			return;
		}
		
		//如果有count怎么办？
		//如果没有count又该怎么办？
		
		$this->sql = $query;
		
		
		array_push($this->deleteSql,
		"delete from little_chuku
		exists (select 1
				from little_main 
				where ($colswhere) in (select $colswhere from little_chuku where fileId = $fileId)
					and little_main.certificationNumber = little_chuku.certificationNumber)"
		);
	}
	
	public function executeDelete(){
		
		try{
			DB::beginTransaction();
			foreach($this->deleteSql as $val){
				DB::query($val);
			}
			DB::commit();
		}catch(Exception $e){
			DB::rollback();
			throw $e;
		}
		
	}

	/**
	 * 执行查询命令，并整理查询的结果
	 *
	 */
	public function execute(){
		$this->result = DB::query($this->sql);
		if($this->result == false){
			return;
		}
		while($row = $this->result->fetch_assoc()){
			array_push($this->certificationNumber, $row['certificationNumber']);
			array_push($this->certificationCodification, $row['certificationCodification']);
			array_push($this->sequenceNumber, $row['sequenceNumber']);
			array_push($this->manufactory, $row['manufactory']);
			array_push($this->authenticationMark, $row['authenticationMark']);
			array_push($this->steelNumber, $row['steelNumber']);	
			array_push($this->size1, $row['size1']);
			array_push($this->size2, $row['size2']);
			array_push($this->size3, $row['size3']);
			array_push($this->count, $row['count']);
			array_push($this->unitWeight, $row['unitWeight']);			
			array_push($this->orderNumber, $row['orderNumber']);
			array_push($this->orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($this->unitPrice,$row['unitPrice']);
			array_push($this->materialNumber, $row['materialNumber']);
			array_push($this->consignmentBatch, $row['consignmentBatch']);
			array_push($this->sampleBatch,$row['sampleBatch']);
			array_push($this->facheNumber,$row['facheNumber']);
			array_push($this->purchaseNumber,$row['purchaseNumber']);
			array_push($this->shipmentDate,$row['shipmentDate']);
			array_push($this->month,$row['month']);
			array_push($this->vehicleNumber, $row['vehicleNumber']);
			array_push($this->consignee, $row['consignee']);
			array_push($this->handler, $row['handler']);
			array_push($this->remark, $row['remark']);
		}
	}
	/**
	 * @return unknown
	 */
	public function getAuthenticationMark() {
		return $this->authenticationMark;
	}
	
	/**
	 * @return unknown
	 */
	public function getCertificationCodification() {
		return $this->certificationCodification;
	}
	
	/**
	 * @return unknown
	 */
	public function getCertificationNumber() {
		return $this->certificationNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getFacheNumber() {
		return $this->facheNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getConsignee() {
		return $this->consignee;
	}
	
	/**
	 * @return unknown
	 */
	public function getConsignmentBatch() {
		return $this->consignmentBatch;
	}
	
	/**
	 * @return unknown
	 */
	public function getCount() {
		return $this->count;
	}
	
	/**
	 * @return int
	 */
	public function getFileId() {
		return $this->fileId;
	}
	
	/**
	 * @return unknown
	 */
	public function getHaveMc() {
		return $this->haveCn;
	}
	
	/**
	 * @return unknown
	 */
	public function getManufactory() {
		return $this->manufactory;
	}
	
	/**
	 * @return unknown
	 */
	public function getMaterialNumber() {
		return $this->materialNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getMonth() {
		return $this->month;
	}
	
	/**
	 * @return unknown
	 */
	public function getOrderNumber() {
		return $this->orderNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getOrderSubitemNumber() {
		return $this->orderSubitemNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getPurchaseNumber() {
		return $this->purchaseNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getRemark() {
		return $this->remark;
	}
	
	/**
	 * @return unknown
	 */
	public function getSampleBatch() {
		return $this->sampleBatch;
	}
	
	/**
	 * @return unknown
	 */
	public function getSequenceNumber() {
		return $this->sequenceNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getShipmentDate() {
		return $this->shipmentDate;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize1() {
		return $this->size1;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize2() {
		return $this->size2;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize3() {
		return $this->size3;
	}
	
	/**
	 * @return unknown
	 */
	public function getSteelNumber() {
		return $this->steelNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitPrice() {
		return $this->unitPrice;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitWeight() {
		return $this->unitWeight;
	}
	
	/**
	 * @return unknown
	 */
	public function getVehicleNumber() {
		return $this->vehicleNumber;
	}

	public function getHandler(){
		return $this->handler;
	}
}
?>